package E2017;

public class Exam3_3 implements Runnable {

    static Integer tickets = 20; // 声明火车票总数

    public static void main(String[] args) {
        Exam3 e = new Exam3();
        new Thread(e, "窗口1").start();
        new Thread(e, "窗口2").start();
        new Thread(e, "窗口3").start();
    }
    @Override
    public void run() {
        while (true) {
            synchronized (tickets) {
                if (tickets > 0) {
                    System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets-- + "张票！");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
